import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Inspect from 'vite-plugin-inspect'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { VantResolver } from '@vant/auto-import-resolver'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Inspect(),
    AutoImport({
      resolvers: [VantResolver()],
      imports: ['vue', 'vue-router'] // 自动引入vue的ref、toRefs、onmounted等，无需在页面中再次引入
    }),
    Components({ // 自动import蚂蚁组件
      resolvers: [
        AntDesignVueResolver({
          importStyle: false, // css in js
        }),
        VantResolver()
      ]
    })
  ],
  base: '/',
  css: {
    preprocessorOptions: {
      less: {
        javascriptEnabled: true // 如果你需要使用 Less 的 JavaScript 表达式，需要设置为 true
      }
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    cors: true, // 默认启用并允许任何源
    open: false, // 在服务器启动时自动在浏览器中打开应用程序
    host: '0.0.0.0',
    // 反向代理配置
    proxy: {
      '/api': {
        target: 'http://218.61.0.145', // 后端地址
        changeOrigin: true,
        ws: true, // 允许websocket代理
        rewrite: (path) => path.replace(/^\/api/, '') // 将'api'替换为空
      }
    }
  }
})
